package mx.com.mindbits.bandpay;



import mx.com.mindbits.bandpay.business.ContactsBusiness;
import mx.com.mindbits.bandpay.dao.ContactsDAO;
import mx.com.mindbits.bandpay.utils.ConstantsValues;
import mx.com.mindbits.bandpay.utils.GenericAlert;
import mx.com.mindbits.bandpay.utils.Utils;
import mx.com.mindbits.bandpay.vo.ContactsVO;
import mx.com.mindbits.dao.contacts;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddContacts extends Activity {
	private EditText nameContact;
	private EditText mailContact;
	private Button btCancel;
	private Button btAcept;
	private Context context;
	private ContactsVO _contactVO;
	private contacts _contact;
	private ContactsDAO _dao;
	private ContactsBusiness _contactBusiness;
	private Bundle bundle;
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_form_contacts);
		context = getApplicationContext();
		_contactVO = new ContactsVO();
		_contactBusiness =  new ContactsBusiness(context);
		_dao = new ContactsDAO(context);
		
		
		nameContact = (EditText) findViewById(R.id.et_name_contact);
		mailContact = (EditText) findViewById(R.id.et_mail_contact);
		btCancel = (Button) findViewById(R.id.bt_cancel);
		btAcept = (Button) findViewById(R.id.bt_acept);
		btAcept.setEnabled(false);
		btAcept.setBackgroundResource(R.drawable.button_disable_shape);
		
		
		
		nameContact.addTextChangedListener(new TextWatcher(){

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				if(nameContact.length() > 0){
					btAcept.setEnabled(true);
					btAcept.setBackgroundResource(R.drawable.blue_button_shape);
				}else{
					btAcept.setEnabled(false);
					btAcept.setBackgroundResource(R.drawable.button_disable_shape);
				}
				
			}
			
		});
		
		
		mailContact.addTextChangedListener(new TextWatcher(){

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				if(Utils.validaCorreo(arg0.toString())){
					btAcept.setEnabled(true);
					btAcept.setBackgroundResource(R.drawable.blue_button_shape);
				}else{
					btAcept.setEnabled(false);
					btAcept.setBackgroundResource(R.drawable.button_disable_shape);
				}
				
			}
			
		});
		
		btCancel.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(context, UserSettings.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);				
			}
		});
		
		btAcept.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				if((nameContact.getText().toString().isEmpty()) ||
						(mailContact.getText().toString().isEmpty())){
					 GenericAlert.generateAlertDialog(
							 context.getString(R.string.text_alert_dialog_contact),
							 context.getString(R.string.msg_alert_dialog_contact),
							 v.getContext()).show();
				}else{
					try{
						settingContactVO();
						if(btAcept.getText().equals("Actualizar")){
							_contactBusiness.updateContact(bundle.getLong("id_contact"), _contactVO);

							Toast.makeText(context, "Contacto Actualizado", Toast.LENGTH_SHORT).show();
						}else {
							insertContact();
						}
						
						Intent intent = new Intent(context, UserSettings.class);
						intent.putExtra("tab", true );
						intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						startActivity(intent);
						}catch(Exception e){
							e.printStackTrace();
							
						}
				}

			}
			
		});
		
	}
	public void settingContactVO(){
		_contactVO.setNombre(nameContact.getText().toString());
		_contactVO.setEmail(mailContact.getText().toString());
		_contactVO.setVisible(true);
	}
	
	public void insertContact(){
		_contactBusiness.insertContact(_contactVO);
		//btAcept.setEnabled(false);
		//btAcept.setBackgroundResource(R.drawable.button_disable_shape);
		Toast.makeText(context, "Contacto insertado", Toast.LENGTH_SHORT).show();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public void onResume(){
		super.onResume();
		bundle = getIntent().getExtras();
		
		if(bundle != null){
			Log.i(ConstantsValues.TAG_DAO, "long " + bundle.getLong("id_contact"));
			
			_contact = _dao.getContactById(bundle.getLong("id_contact"));
			nameContact.setText(_contact.getContact_name());
			mailContact.setText(_contact.getContact_email());
			btAcept.setText("Actualizar");
			
		}
		
	}

}
